package q1302_deepestLeavesSum;

import CommonClass.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

public class Solution {
    /*
    最简单的层序遍历 只需要维护一个值记录每一层的和 最后一个肯定是最后一层的
     */
    public int deepestLeavesSum(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int ans = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            ans = 0;
            for (int i = 0; i < size; ++i) {
                TreeNode node = queue.poll();
                ans += node.val;
                if (node.left != null) queue.offer(node.left);
                if (node.right != null) queue.offer(node.right);
            }

        }
        return ans;
    }
}
